package cc.telecomdigital.tdstock.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import cc.telecomdigital.tdstock.BitmapResizer;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

/**
 * ProgressBar SunFlower View Animation
 * 
 * @author ouyangzhihui
 */
public class SunFlowerView extends View
{
	/** The view bounds. */
	private Bitmap					bm;

	private float					angle;
	
	private final int   DEFAULT_IMG_SIZE = 35;

	private int						bmWidth						= DEFAULT_IMG_SIZE;
	private int						bmHeight					= DEFAULT_IMG_SIZE;

	private boolean					stopFlag					= true;		// 线程是否停下的标识。最初，没有线程在跑

	private float					x;

	private float					y;

	private final int				SUNFLOWER_SLEEP_TIME		= 2400 / 18;

	private final int				SUNFLOWER_ROTATE_ANGLE_STEP	= 360 / 12;

	private boolean					isScaleTooSmall				= false;

	private Paint					paint/* = new Paint() */;
	private PaintFlagsDrawFilter	pfd;
	private static SunFlowerView	instance					= null;

	public SunFlowerView(Context context)
	{
		super(context);

		/*bm = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.nvchart_sunflower);*/
		bm = BitmapResizer.decodeImage(context.getResources(),
				R.drawable.nvchart_sunflower, DEFAULT_IMG_SIZE);
		bmWidth = bm.getWidth();
		bmHeight = bm.getHeight();
		paint = new Paint();
		paint.setAntiAlias(true); // modified by haibo, 2011-06-13, 图片抗锯齿效果
		pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
				| Paint.FILTER_BITMAP_FLAG); // modified by haibo, 2011-06-13,
		// 图片抗锯齿效果
	}

	public SunFlowerView(Context context, AttributeSet arttr)
	{
		super(context, arttr);

		/*bm = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.nvchart_sunflower);*/
		
		bm = BitmapResizer.decodeImage(context.getResources(),
				R.drawable.nvchart_sunflower, DEFAULT_IMG_SIZE);
		
		bmWidth = bm.getWidth();
		bmHeight = bm.getHeight();
		paint = new Paint();
		paint.setAntiAlias(true); // modified by haibo, 2011-06-13, 图片抗锯齿效果
		pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
				| Paint.FILTER_BITMAP_FLAG); // modified by haibo, 2011-06-13,
		// 图片抗锯齿效果
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		super.onSizeChanged(w, h, oldw, oldh);
		changeSize(w, h);

		TDStockLog.d("SunFlower", "sunflowerview   w:" + w + "  	h:" + h);
	}

	public void startAnimation()
	{
		TDStockLog.d("SunFlower", "start");
		if (!stopFlag || isScaleTooSmall)
		{ // 如果当前有线程在更新，不再开启新的线程
			return;
		}

		Thread sunflowerThread = new Thread()
		{
			public void run()
			{
				super.run();
				TDStockLog.d("SunFlower", "start");

				stopFlag = false;

				while (!stopFlag && !Thread.interrupted())
				{
					angle = (angle + SUNFLOWER_ROTATE_ANGLE_STEP) % 360;
					try
					{
						postInvalidate();

						Thread.sleep(SUNFLOWER_SLEEP_TIME);
					}
					catch (Exception e)
					{
						stopFlag = true;
					}
				}
				stopFlag = true;
			}
		};

		sunflowerThread.setName("sunflowerThread");

		sunflowerThread.start();
	}

	public void stopAnimation()
	{
		stopFlag = true;

		TDStockLog.d("SunFlower", "stop");
	}

	protected void changeSize(int width, int height)
	{
		x = width / 2.0f;
		y = height / 2.0f;

		if (bm.getWidth() > width * 0.8f)
		{

			bmWidth = (int) (width * 0.8f);

			bmHeight = bmWidth;

			if (bmWidth <= 0)
			{
				isScaleTooSmall = true;
				return;
			}
			else
			{
				isScaleTooSmall = false;
			}

			Bitmap temp = bm;
			this.bm = Bitmap.createScaledBitmap(temp, bmWidth, bmHeight,
					false);

			temp.recycle();
		}

		if (bm.getHeight() > height * 0.8f)
		{

			bmHeight = (int) (height * 0.8f);
			bmWidth = bmHeight;

			if (bmWidth <= 0)
			{
				isScaleTooSmall = true;
				return;
			}
			else
			{
				isScaleTooSmall = false;
			}

			Bitmap temp = bm;
			this.bm = Bitmap.createScaledBitmap(temp, bmWidth, bmHeight,
					false);

			temp.recycle();
		}
	}

	public boolean isRunning()
	{
		boolean result = (!stopFlag && !Thread.interrupted());
		return result;
	}

	@Override
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		canvas.setDrawFilter(pfd); // modified by haibo, 2011-06-13, 图片抗锯齿效果
		canvas.rotate(angle, x, y);
		canvas.drawBitmap(bm, x - bmWidth / 2.0f, y - bmHeight / 2.0f, paint);
	}

	protected static WindowManager.LayoutParams getWMLayoutParams()
	{
		WindowManager.LayoutParams wl = new WindowManager.LayoutParams();

		wl.x = 0;
		wl.y = 0;

		wl.height = WindowManager.LayoutParams.WRAP_CONTENT;
		wl.width = WindowManager.LayoutParams.WRAP_CONTENT;
		wl.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		wl.format = PixelFormat.TRANSLUCENT;
		wl.windowAnimations = 0;
		return wl;
	}

	public static void showInScreenCenter(Context context)
	{
		if (instance == null)
		{
			instance = new SunFlowerView(context);
			instance.startAnimation();
			instance.setVisibility(View.VISIBLE);
		}

		try
		{
			WindowManager atWindowManager = (WindowManager) context
					.getSystemService(Context.WINDOW_SERVICE);

			atWindowManager.addView(instance, getWMLayoutParams());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	// notes, only hide the one that call showInScreenCenter;
	public static void hide(Context context)
	{

		try
		{
			if (instance != null)
			{
				instance.setVisibility(View.GONE);
				instance.stopAnimation();
				WindowManager atWindowManager = (WindowManager) context
						.getSystemService(Context.WINDOW_SERVICE);
				if (atWindowManager != null && instance != null)
				{
					atWindowManager.removeView(instance);
					instance = null;
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			instance = null;
		}
	}
}